import java.util.ArrayList;
import java.util.List;

public class project {
    public List<Integer> pancakeSort(int[] arr) {
        int len=arr.length;
        int n=len;
        int index=len-1;
        int x=len;
        List<Integer> list=new ArrayList<>();
        while (x!=1){
            int k=0;
            while (true){
                if(arr[k]==x){
                    break;
                }
                k++;
            }
            if(k!=index){
                if(k!=0){
                    reverse(arr,0,k);
                    list.add(k+1);
                }
                reverse(arr,0,index);
                list.add(index+1);
            }
            index--;
            x--;
        }
        return list;
    }
    public void reverse(int[] arr,int left,int right){
        while (left<right){
            int tmp=arr[left];
            arr[left]=arr[right];
            arr[right]=tmp;
            left++;
            right--;
        }
    }
}
